package com.skb.task.controller;

import com.skb.task.pojo.Project;
import com.skb.task.service.ProjectService;
import com.skb.task.util.Constant;
import com.skb.task.util.pojo.QdResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 沈楷博
 * @Company 永遠快樂
 * @Descriptiond
 * @Time 2024/9/5 下午5:15
 */
@RestController
@RequestMapping("/project")
@CrossOrigin(origins = "http://localhost:5173")

public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @GetMapping("/list")
    public QdResult projectList(Project project, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        System.out.println(project.getStartTimeLeft());
        project.setUserId(1L);
        return projectService.queryByProjectPage(project, page, limit);
    }


    @GetMapping("/projectNum")
    public QdResult projectNum(String projectId){
        return projectService.queryAllProject(projectId);
    }


    @GetMapping("/myProjectNum")
    public QdResult myProjectNum(String userId){
        return projectService.queryMyProject(Long.valueOf(userId));
    }

    @GetMapping("/myList")
    public QdResult projectMyList(Project project, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,@RequestParam(required = false) Long userId) {
        System.out.println(project.getStartTimeLeft());
        if (userId != null) {
            project.setUserId(userId);
        }
        return projectService.queryByProjectPageById(project, page, limit,userId);
    }

    @GetMapping("/get")
    public QdResult get(String projectId){
        return projectService.queryById(projectId);
    }

    @GetMapping("/pigeonholeList")
    public QdResult projectPigeonholeList(Project project, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,@RequestParam(required = false) Long userId) {
        System.out.println(project.getStartTimeLeft());
        if (userId != null) {
            project.setUserId(userId);
        }
        return projectService.queryByProjectPageByPigeonhole(project, page, limit,userId);
    }
    @GetMapping("/deleteList")
    public QdResult projectDeleteList(Project project, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,@RequestParam(required = false) Long userId) {
        System.out.println(project.getStartTimeLeft());
        if (userId != null) {
            project.setUserId(userId);
        }
        return projectService.queryByProjectPageByDelete(project, page, limit,userId);
    }


    @PostMapping("/add")
    public QdResult save(@RequestBody Project project) {
        try {
            // 验证开始时间是否早于结束时间
            if (project.getStartTime() != null && project.getEndTime() != null && project.getEndTime().before(project.getStartTime())) {
                return QdResult.error("结束时间不能早于开始时间");
            }
            // 设置默认状态为草稿
            project.setProjectStatus(1);
            projectService.save(project);
            return QdResult.success("项目添加成功", project.getProjectStatus());
        } catch (Exception e) {
            return QdResult.error(-1, e.getMessage()); // 使用合适的错误码
        }
    }

    @GetMapping("/projectId")
    public QdResult getProjectDetails(String projectId) {
        Project project = projectService.findProjectById(projectId);
        if (project != null) {
            return QdResult.success(project);
        } else {
            return QdResult.error(Constant.RESPONSE_STATUS_NOT_FOUND, "项目未找到");
        }
    }

    //修改
    @PutMapping("/update")
    public QdResult update(@RequestBody Project project) {
        return projectService.updateProject(project);
    }

    //删除
    @DeleteMapping("/delete")
    public QdResult delete(String projectId) {
        return projectService.deleteById(projectId);
    }

    //归档
    @PutMapping("/file")
    public QdResult file(String projectId) {
        return projectService.fileById(projectId);
    }

    //恢复
    @PutMapping("/recovery")
    public QdResult recovery(String projectId) {
        return projectService.recoveryById(projectId);
    }

    @PostMapping("/publish")
    public QdResult publish(@RequestBody String[] projectIds) {
        return projectService.publishProject(projectIds);
    }

    //批量重新发布
    @PostMapping("/filePublish")
    public QdResult filePublish(@RequestBody String[] projectIds) {
        return projectService.filePublishProject(projectIds);
    }


    //批量恢复
    @PostMapping("/deleteRecovery")
    public QdResult deleteRecovery(@RequestBody String[] projectIds) {
        return projectService.deleteRecoveryProject(projectIds);
    }


    //批量归档
    @PutMapping("/archiving")
    public QdResult archiving(@RequestBody String[] projectIds) {
        return projectService.archivingProject(projectIds);
    }

    //批量删除
    @DeleteMapping("/deletes")
    public QdResult deletes(@RequestBody String[] projectIds){
        return projectService.deletionProject(projectIds);
    }


    @DeleteMapping("/fileDeletes")
    public QdResult fileDeletes(@RequestBody String[] projectIds) {
        return projectService.fileDeletionProject(projectIds);
    }


}